package com.wzk.travelspringboot.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.entity.User;
import com.wzk.travelspringboot.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author wangzikang
 */
@Slf4j
@RestController
@RequestMapping("/admin/user")
@RequiredArgsConstructor
@Validated
public class AdminUserController {

    private final UserService userService;

    /**
     * 分页查询所有用户信息
     */
    @GetMapping("/list")
    public Result listUsers(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        Page<User> userPage = userService.page(page);
        return Result.success(userPage);
    }

    /**
     * 根据ID查询用户详情
     */
    @GetMapping("/detail/{id}")
    public Result getUserDetail(@PathVariable("id") @NotNull(message = "用户ID不能为空") Long id) {
        User user = userService.getById(id);
        if (user == null) {
            return Result.failed("用户不存在");
        }
        // 出于安全考虑，返回前清除敏感信息
        user.setPassword(null);
        return Result.success(user);
    }

    /**
     * 更新用户状态（启用/禁用）
     */
    @PostMapping("/state/{id}/{state}")
    public Result updateUserState(
            @PathVariable("id") @NotNull(message = "用户ID不能为空") Long id,
            @PathVariable("state") @NotNull(message = "状态不能为空") Integer state) {
        User user = new User();
        user.setId(id);
        user.setState(state);
        boolean success = userService.updateById(user);
        return success ? Result.success("更新用户状态成功") : Result.failed("更新用户状态失败");
    }

    /**
     * 更新用户角色
     */
    @PostMapping("/role/{id}/{role}")
    public Result updateUserRole(
            @PathVariable("id") @NotNull(message = "用户ID不能为空") Long id,
            @PathVariable("role") @NotNull(message = "角色不能为空") Integer role) {
        User user = new User();
        user.setId(id);
        user.setRole(role);
        boolean success = userService.updateById(user);
        return success ? Result.success("更新用户角色成功") : Result.failed("更新用户角色失败");
    }

    /**
     * 根据昵称模糊查询用户
     */
    @GetMapping("/search")
    public Result searchUserByNickName(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String nickName) {
        Page<User> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getNickName, nickName);
        Page<User> userPage = userService.page(page, queryWrapper);
        return Result.success(userPage);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/remove/{id}")
    public Result deleteUser(@PathVariable("id") @NotNull(message = "用户ID不能为空") Long id) {
        boolean success = userService.removeById(id);
        return success ? Result.success("删除用户成功") : Result.failed("删除用户失败");
    }
} 